package com.dd.census.controller;

import java.util.Date;
import com.dd.census.controller.common.BaseController;
import com.dd.census.entity.Device;
import com.dd.census.service.DeviceService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.validator.ValidateUtil.ValidationResult;
import com.dd.census.tools.validator.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("rest/device")
public class DeviceController extends BaseController  {
	private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);
	@Autowired
	private DeviceService deviceService;

	@RequestMapping("selectPage")
	public ResponseEntity<Object> select(@RequestBody Device device, HttpServletRequest request) {
		try {
			return this.doSuccess(request, deviceService.selectPage(device));
		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResponseEntity<Object> insert(HttpServletRequest request, @RequestBody Device device) {

		try {
			// 基本输入验证
			ValidationResult rscheck = ValidateUtil.validate(device);
			if(!rscheck.isHasErrors()) {
				device.setCreateUserId(this.getUserId(request));
				device.setCreateTime(new Date());
				deviceService.insert(device);
				return this.doSuccess(request);
			}else {
				return this.doSuccess(request,rscheck);
			}
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("/getById/{id}")
	public ResponseEntity<Object> getById(@PathVariable("id") String id, HttpServletRequest request) {
		try {
			return this.doSuccess(request, deviceService.selectByKey(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("updateDevice")
	public ResponseEntity<Object> update(HttpServletRequest request, @RequestBody Device device) {

		try {
			// 基本输入验证
			ValidationResult rscheck =ValidateUtil.validate(device);
			device.setUpdateUserId(this.getUserId(request));
			device.setUpdateTime(new Date());
			if(!rscheck.isHasErrors()) {
				deviceService.updateDevice(device);
				return this.doSuccess(request);
			}else {
				return this.doSuccess(request,rscheck);
			}

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("delete")
	public ResponseEntity<Object> delete(HttpServletRequest request, @RequestBody Device device) {

		try {
			deviceService.delete(device);

			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}